﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kira.Compiler.Bods
{
    [XmlElementMapping("DITransformCall", "name", "Pivot")]
    public class DIPivot : BodsTransform
    {
        public String InputName
        {
            get
            {
                return this.GetElement("DIInputView").GetAttrbuteValue("name");
            }
        }

        public String OutputName
        {
            get
            {
                return this.GetElement("DISchema").GetAttrbuteValue("name");
            }
        }

        public IEnumerable<DIElement> Columns
        {
            get
            {
                return this.GetElement("DISchema").GetElements<DIElement>();
            }
        }

        public IEnumerable<String> NonPivotColumnNames
        {
            get
            {
                return from c in this.GetElement("DIAttributes").GetElements(c => c.GetAttrbuteValue("name").Contains("nonpivot_column")) select c.GetAttrbuteValue("value");
            }
        }

        public IEnumerable<String> PivotColumnNames
        {
            get
            {
                return from c in this.GetElement("DIAttributes").GetElements(c => c.GetAttrbuteValue("name").Contains("pivot_column")) select c.GetAttrbuteValue("value");
            }
        }

        public IEnumerable<String> DataFieldNames
        {
            get
            {
                return from c in this.GetElement("DIAttributes").GetElements(c => c.GetAttrbuteValue("name").Contains("pivot_data_field")) select c.GetAttrbuteValue("value");
            }
        }

        public IEnumerable<String> HeaderNames
        {
            get
            {
                return from c in this.GetElement("DIAttributes").GetElements(c => c.GetAttrbuteValue("name").Contains("pivot_header_name")) select c.GetAttrbuteValue("value");
            }
        }

        public String SequenceName
        {
            get
            {
                return this.GetElement("DIAttributes").GetElement(c => c.GetAttrbuteValue("name") == "pivot_sequence_name").GetAttrbuteValue("value");
            }
        }

        public int GroupCount
        {
            get
            {
                return this.GetElement("DIAttributes").GetElement(c => c.GetAttrbuteValue("name") == "total_pivot_groups").GetAttrbuteIntegerValue("value");
            }
        }

        public override IEnumerable<string> InputNames
        {
            get { return new String[] { this.InputName }; }
        }

        public override IEnumerable<string> OutputNames
        {
            get { return new String[] { this.OutputName }; }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GetComments("Pivot"));
            sb.Append("WITH ").Append(OutputName).AppendLine(" AS (SELECT ")
                .Append(Columns.Select(c => c.ToString()).Aggregate((a, b) => a + ",\n")).Append(" FROM ").Append(InputName).AppendLine(");");
            return sb.ToString();
        }
    }
}
